package com.cskaoyan.market.controller;

import com.cskaoyan.market.service.MarketConfigService;
import com.cskaoyan.market.service.MarketConfigServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;


import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;


@WebServlet("/admin/config/*")
public class AdminConfigController extends HttpServlet {
    private MarketConfigService configService = new MarketConfigServiceImpl();

    //get查询
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/config/", "");
        if ("express".equals(op)) {
            express(req, resp);
        }
        else if ("order".equals(op)) {
            order(req, resp);
        }
        else if ("mall".equals(op)) {
            mall(req, resp);
        }
        else if ("wx".equals(op)) {
            wx(req, resp);
        }
    }

//post更新
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        update(req, resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        BufferedReader reader = req.getReader();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        Map<String, Object> map = objectMapper.readValue(reader, Map.class);
        configService.update(map);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));
    }

    private void mall(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, String> data = configService.mall();
        String response = JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(data));
        resp.getWriter().println(response);
    }


    private void wx(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, String> data = configService.wx();
        String response = JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(data));
        resp.getWriter().println(response);
    }


    private void order(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, String> data = configService.order();
        String response = JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(data));
        resp.getWriter().println(response);
    }

    private void express(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, String> data = configService.express();
        String response = JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok(data));
        resp.getWriter().println(response);
    }
}

